# ~*~ coding: utf-8 ~*~
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms.validators import InputRequired

from apps.common.form import ErrorFormMinx

__all__ = ['ProjectForm', 'PermissionForm']


class ProjectForm(ErrorFormMinx, FlaskForm):
    name = StringField(validators=[InputRequired(message="项目名称不能为空")])
    environment_id = StringField(validators=[InputRequired(message="请选择的所属环境")])
    repo_url = StringField(validators=[InputRequired(message="仓库Url不能为空")])
    repo_username = StringField()
    repo_password = StringField()
    repo_mode = StringField()
    repo_type = StringField()
    server_ids = StringField(validators=[InputRequired(message="目标主机不能为空")])
    target_root = StringField(validators=[InputRequired(message="目标主机的web目录不能为空")])
    target_releases = StringField(validators=[InputRequired(message="目标主机的Release目录不能为空")])
    vars = StringField()
    keep_version_num = StringField()
    prev_deploy = StringField()
    post_deploy = StringField()
    prev_release = StringField()
    post_release = StringField()
    task_audit = StringField()
    status = StringField()
    label_id = StringField()
    privilege_user = StringField()
    sparse_dir = StringField()
    describe = StringField()
    filter_files = StringField()

    class Meta:
        csrf = True

    @property
    def data(self):
        return {
            'name': self.name.data.strip(),
            'environment_id': self.environment_id.data.strip(),
            'repo_url': self.repo_url.data.strip(),
            'repo_username': self.repo_username.data.strip(),
            'repo_password': self.repo_password.data.strip(),
            'repo_mode': self.repo_mode.data.strip(),
            'repo_type': self.repo_type.data.strip(),
            'server_ids': self.server_ids.data.strip(),
            'target_root': self.target_root.data.strip(),
            'target_releases': self.target_releases.data.strip(),
            'vars': self.vars.data.strip(),
            'keep_version_num': self.keep_version_num.data.strip(),
            'prev_deploy': self.prev_deploy.data.strip(),
            'post_deploy': self.post_deploy.data.strip(),
            'prev_release': self.prev_release.data.strip(),
            'post_release': self.post_release.data.strip(),
            'task_audit': self.task_audit.data.strip(),
            'status': self.status.data.strip(),
            'label_id': self.label_id.data.strip(),
            'privilege_user': self.privilege_user.data.strip(),
            'sparse_dir': self.sparse_dir.data.strip(),
            'describe': self.describe.data.strip(),
            'filter_files': self.filter_files.data.strip()
        }


class PermissionForm(ErrorFormMinx, FlaskForm):
    project_id = StringField()
    user_ids = StringField()

    class Meta:
        csrf = True

    @property
    def data(self):
        return {
            'project_id': self.project_id.data,
            'user_ids': self.user_ids.data,
        }
